package com.smoed.common.utilities;

public class ArrayMath {

    /*
     * Smoothes an array of doubles by averaging each value in the array with its
     * surrounding values.  Radius defines how many points around the value
     * to include in the calculation
     */
    public synchronized static double[] getSmoothedArraySimple(double[] data, int radius) {
        double[] smoothedData = new double[data.length];

        for (int i = 0; i < data.length; i++) {

            int pointsInCalculation = 0;
            double sum = 0;
            int j = (i - radius < 0) ? 0 : i - radius;

            for (; j <= i + radius && j < data.length; j++) {
                sum += data[j];
                pointsInCalculation++;
            }

            smoothedData[i] = sum / pointsInCalculation;
        }

        return smoothedData;
    }
}
